/**
 * Yolema.com Inc.
 * Copyright (c) 2011-2012 All Rights Reserved.
 */
package org.grayrabbit.pay.chinapay;

import org.grayrabbit.pay.chinapay.config.ChinaPayConfigUtil;
import org.grayrabbit.pay.trade.TradeChinaPayInfo;

import chinapay.SecureLink;

/**
 * 
 * @author wy
 * @version $Id: ChinaPayTrade.java, v 0.1 2012-6-4 下午2:36:43 wy Exp $
 */
public class ChinaPay {

    /**
     * 交易订单签名
     * 
     * @param tradeChinaPayInfo
     */
    public static String signOrder(TradeChinaPayInfo tradeChinaPayInfo) {
        //交易日期,只能为当天
        SecureLink secureLink = new SecureLink(PayBuild.buildMer());
        String checkValue = secureLink.signOrder(ChinaPayConfigUtil.getChinaPayInfo().getMemId(),
            tradeChinaPayInfo.getTradeId(), tradeChinaPayInfo.getMoney().getChinaPay(),
            tradeChinaPayInfo.getCurrencyCode().getCode(), tradeChinaPayInfo.getTransDate(),
            tradeChinaPayInfo.getPayTradeType().getCode());
        tradeChinaPayInfo.setCheckValue(checkValue);
        return checkValue;
    }

    /**
     * 退款签名
     * 
     * @param signString
     * @return
     */
   public static String sign(String signString){
       SecureLink secureLink = new SecureLink(PayBuild.buildMer());
       return secureLink.Sign(signString);
   }
    
    /**
     * 验证交易应答函数
     * 
     * @param tradeChinaPayInfo
     * @return
     */
    public static boolean verifyTransResponse(TradeChinaPayInfo tradeChinaPayInfo) {
        SecureLink secureLink = new SecureLink(PayBuild.buildPub());
        return secureLink.verifyTransResponse(ChinaPayConfigUtil.getChinaPayInfo().getMemId(),
            tradeChinaPayInfo.getTradeId(), tradeChinaPayInfo.getMoney().getChinaPay(),
            tradeChinaPayInfo.getCurrencyCode().getCode(), tradeChinaPayInfo.getTransDate(),
            tradeChinaPayInfo.getPayTradeType().getCode(), tradeChinaPayInfo.getOrderStatus(),
            tradeChinaPayInfo.getCheckValue());
    }
    
    /**
     * 验证银联数据
     * 
     * @param plainData
     * @param checkValue
     * @return
     */
    public static boolean verifyAuthToken(String plainData, String checkValue){
        SecureLink secureLink = new SecureLink(PayBuild.buildPub());
        return secureLink.verifyAuthToken(plainData, checkValue);
    }
    

}
